function Node(value) {
  this.value = value;
  this.left = null;
  this.right = null;
}

var a1 = new Node("a");
var b1 = new Node("b");
var c1 = new Node("c");
var d1 = new Node("d");
var e1 = new Node("e");
var f1 = new Node("f");
var g1 = new Node("g");

a1.left = c1;
a1.right = b1;
c1.left = f1;
// c1.right = g1;
b1.left = d1;
b1.right = e1;

var a2 = new Node("a");
var b2 = new Node("b");
var c2 = new Node("c");
var d2 = new Node("d");
var e2 = new Node("e");
var f2 = new Node("f");
var g2 = new Node("g");

a2.right = c2;
a2.left = b2;
c2.left = f2;
// c2.right = g2;
b2.left = d2;
b2.right = e2;
// a
// c   b 
//f   d e
console.log(a1)
console.log(a2)
// debugger
function compare(tree1,tree2){
  if(tree1 ==tree2 ) return true
  if(tree1 == null && tree2 != null || tree1 != null && tree2 == null) return false
  
  if(tree1.value != tree2.value ) return false
  let left = compare(tree1.left,tree2.left) 
  
  let right = compare(tree1.right,tree2.right) 

  return left && right
  
}
console.log(compare(a1,a2))


// 二叉树比较问题 ， 必须要确定，左右两颗子树如果交换位置 是否算 同一个二叉树
// 笔试的话 没有特殊说明左后互换还是同一颗树，那么默认互换不是同一颗树
// 严格上说不算 从拓扑树来说 是算，面试可以问下尽量


function compare2(tree1,tree2){
  if(tree1 ==tree2 ) return true
  if(tree1 == null && tree2 != null || tree1 != null && tree2 == null) return false
  
  if(tree1.value != tree2.value ) return false
  let left = compare(tree1.left,tree2.left) || compare(tree1.right,tree2.left)
  
  let right = compare(tree1.right,tree2.right) || compare(tree1.left,tree2.right)

  return left && right
  
}
console.log(compare2(a1,a2))

